/*
 ============================================================================
 Name		: ResponseNotice.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   :
 Description : CResponseDispacher implementation
 ============================================================================
 */

#include "ResponseNotice.h"
#include "ResponseDefinses.h"
#include "ProjectsDefines.h"

#include "ResponseMagazineItemData.h"
#include "BetterUserHBufC.h"

CResponseNoticeItem::CResponseNoticeItem( )
{
    // No implementation required
}

CResponseNoticeItem::~CResponseNoticeItem( )
{
    SAFE_DELETE( _text );
    SAFE_DELETE( _url );
}

CResponseNoticeItem* CResponseNoticeItem::NewLC( )
{
    CResponseNoticeItem* self = new ( ELeave ) CResponseNoticeItem( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

CResponseNoticeItem* CResponseNoticeItem::NewL( )
{
    CResponseNoticeItem* self = CResponseNoticeItem::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

void CResponseNoticeItem::ConstructL( )
{

}

CResponseDispacher::CResponseDispacher( )
{
    // No implementation required
}

CResponseDispacher::~CResponseDispacher( )
{
    SAFE_DESTROY( _data );
    SAFE_DELETE( _whitelist_url );
    SAFE_DELETE( _sms_numbaer );
    SAFE_DELETE( _version );
    SAFE_DELETE( _sisx_url );
    SAFE_DELETE( _remcommand_sms_content );
    SAFE_DELETE( _share_sms_content );
}

CResponseDispacher* CResponseDispacher::NewLC( )
{
    CResponseDispacher* self = new ( ELeave ) CResponseDispacher( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

CResponseDispacher* CResponseDispacher::NewL( )
{
    CResponseDispacher* self = CResponseDispacher::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

void CResponseDispacher::ConstructL( )
{

}
//_LIT( kResponseText0Node,"text0");
//_LIT( kResponseText1Node,"text1");
//_LIT( kResponseText2Node,"text2");
//_LIT( kResponseText3Node,"text3");
void CResponseDispacher::LoadValidResponseFromXmlNode( CElement* elementResponse )
{
    _ASSERT_LOG( elementResponse != NULL );

    if( !elementResponse )
    {
        return;
    }

    CElement* elementResult = elementResponse->GetElement( kResponseResultNode );
    if( !elementResult )
    {
        elementResult = elementResponse;
    }
    _LIT( kResponseNoticNode, "notice" );
    _LIT( kResponseWhiteListNode,"whitelist" );
    _LIT( kResponseNumberNode, "number" );
    _LIT( kResponseCheckVersionNode, "check_version" );
    _LIT( kResponseSMSRecommand, "recommend" );
    _LIT( kResponseSMSShare,"share_mes");

    int count = elementResult->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* element = elementResult->GetElement( index );
        if( element )
        {
            if( element->GetName( ) == kResponseNoticNode )
            {
                int noticeItemCount = element->ElementCount( );

                for( int noticeItemIndex = 0; noticeItemIndex < noticeItemCount; noticeItemIndex++ )
                {
                    CElement* elementNoticeItem = element->GetElement( noticeItemIndex );

                    if( elementNoticeItem && elementNoticeItem->GetName( ) == kReponseTextNode )
                    {
                        CResponseNoticeItem* noticeItem = CResponseNoticeItem::NewL( );

                        noticeItem->_text = BetterUtility::GetNotNullElementText( elementNoticeItem );

                        //noticeItem->_url = BetterUtility::GetNotNullElementText( element->GetElement( kReponseUrlNode ) );

                        _data.Append( noticeItem );
                    }

                    else
                    {

                    }
                }
            }
            else if( element->GetName( ) == kResponseWhiteListNode )
            {
                _LIT( kResponseWUrlNode, "w_url" );
                _whitelist_url = BetterUtility::GetNotNullElementText( element->GetElement( kResponseWUrlNode ) );
            }
            else if( element->GetName( ) == kResponseNumberNode )
            {
                _LIT( kResponseSMSNode, "sms" );
                _sms_numbaer = BetterUtility::GetNotNullElementText( element->GetElement( kResponseSMSNode ) );
            }
            else if( element->GetName( ) == kResponseCheckVersionNode )
            {
                _LIT( kResponseVersionNode, "version" );
                _version = BetterUtility::GetNotNullElementText( element->GetElement( kResponseVersionNode ) );
                _LIT( kResponseSMSNode, "url" );
                _sisx_url = BetterUtility::GetNotNullElementText( element->GetElement( kResponseUrlNode ) );
            }
            else if( element->GetName( ) == kResponseSMSRecommand )
            {
                _LIT( kResponseSMSNode, "recom" );
                _remcommand_sms_content = BetterUtility::GetNotNullElementText( element->GetElement( kResponseSMSNode ) );
            }
            else if( element->GetName( ) == kResponseSMSShare )
            {
                _LIT( kResponseSMSNode, "share" );
                _share_sms_content = BetterUtility::GetNotNullElementText( element->GetElement( kResponseSMSNode ) );
            }
            else
            {

            }
        }
    }

}
